package com.terabits.smartriver.controller;

import com.alibaba.fastjson.JSONArray;
import com.terabits.smartriver.constants.result.BaseResp;
import com.terabits.smartriver.constants.result.ResultStatus;
import com.terabits.smartriver.mapper.DeviceMongoDataMapper;
import com.terabits.smartriver.meta.dto.MongoData;
import com.terabits.smartriver.meta.po.Model;
import com.terabits.smartriver.meta.po.ModelSetting;
import com.terabits.smartriver.service.ModelService;
import com.terabits.smartriver.util.PageData;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 模型管理接口
 */
@CrossOrigin
@RestController
public class ModelController extends BaseController {
    @Autowired
    private ModelService modelService;
    @Autowired
    private DeviceMongoDataMapper deviceMongoDataMapper;

    @ApiOperation(value = "查看 模型", tags = {"模型管理"})

    @RequestMapping(value = "/model-mgt/model/get", method = RequestMethod.GET)
    public BaseResp getModel(Integer modelId) {
        return new BaseResp(ResultStatus.SUCCESS, modelService.getModel(modelId));
    }

//    @RequestMapping(value = "/aa", method = RequestMethod.GET)
//    public String aa() {
//        deviceMongoDataMapper.addDeviceMongoData("aa", "111111", "1000", "1000", "2019-08-01");
//        return "success";
//    }

    @ApiOperation(value = "通过项目名称查询 模型", tags = {"模型管理"})

    @RequestMapping(value = "/model-mgt/model/getModelByProject", method = {RequestMethod.POST})
    public BaseResp getModelByProject() {
        List getModelByProject = modelService.getModelByProject(this.getPageData());
        if (getModelByProject != null && getModelByProject.size() != 0) {
            return new BaseResp(ResultStatus.SUCCESS, getModelByProject);
        }
        return new BaseResp(ResultStatus.error_record_not_found);
    }

    @ApiOperation(value = "添加 模型", tags = {"模型管理"})
    @RequestMapping(value = "/model-mgt/model/add", method = RequestMethod.POST)
    public BaseResp addModel(Model model) {
//        expression = "result=Math.pow(calculation.conver(\"14427033\") * (calculation.conver(\"14427055\") + 1),2)";
        int res = modelService.addModel(model);
        if (res == -1) {
            return new BaseResp(ResultStatus.error_invalid_argument);
        }
        return new BaseResp(ResultStatus.SUCCESS, model.getId());
    }

    @ApiOperation(value = "修改 模型", tags = {"模型管理"})
    @RequestMapping(value = "/model-mgt/model/update", method = RequestMethod.POST)
    public BaseResp updateModel(Model model) {
        return new BaseResp(ResultStatus.SUCCESS, modelService.updateModel(model));
    }

    @ApiOperation(value = "添加模型结果阈值", tags = {"模型管理"})
    @RequestMapping(value = "/model-mgt/model/setting/add", method = RequestMethod.POST)
    public BaseResp addModelSetting(@RequestParam(value = "modelSettingsJSON") String modelSettingsJSON) {
        List<ModelSetting> modelSettings = JSONArray.parseArray(modelSettingsJSON, ModelSetting.class);
        return new BaseResp(ResultStatus.SUCCESS, modelService.addModelSetting(modelSettings));
    }

    @ApiOperation(value = "修改模型结果阈值", tags = {"模型管理"})
    @RequestMapping(value = "/model-mgt/model/setting/update", method = RequestMethod.POST)
    public BaseResp updateModelSetting(ModelSetting modelSetting) {
//        List<ModelSetting> modelSettings = JSONArray.parseArray(modelSettingsJSON, ModelSetting.class);
        List<ModelSetting> modelSettings = new ArrayList<>();
        modelSettings.add(modelSetting);
        return new BaseResp(ResultStatus.SUCCESS, modelService.updateModelSetting(modelSettings));

    }

    @ApiOperation(value = "删除模型结果阈值", tags = {"模型管理"})
    @RequestMapping(value = "/model-mgt/model/setting/delete", method = RequestMethod.POST)
    public BaseResp deleteModelSetting(@RequestParam("ids") List<Integer> ids) {
        return new BaseResp(ResultStatus.SUCCESS, modelService.deleteModelSetting(ids));

    }

    @ApiOperation(value = "查找模型-设备绑定", tags = {"模型管理"})
    @RequestMapping(value = "/model-mgt/model/modeldevice/get", method = RequestMethod.POST)
    public BaseResp getModelDevice() {
        return new BaseResp(ResultStatus.SUCCESS, modelService.getModelDevice(this.getPageData()));
    }

    @ApiOperation(value = "添加模型-设备绑定", tags = {"模型管理"})
    @RequestMapping(value = "/model-mgt/model/modeldevice/add", method = RequestMethod.POST)
    public BaseResp addModelDevice() {
        int i = modelService.addModelDevice(this.getPageData());
        if (i == 0) {
            return new BaseResp(ResultStatus.FAIL);
        }
        return new BaseResp(ResultStatus.SUCCESS);
    }

    @ApiOperation(value = "修改模型-设备绑定", tags = {"模型管理"})
    @RequestMapping(value = "/model-mgt/model/modeldevice/update", method = RequestMethod.POST)
    public BaseResp updateModelDevice() {
        return new BaseResp(ResultStatus.SUCCESS, modelService.updateModelDevice(this.getPageData()));
    }

    @ApiOperation(value = "删除模型-设备绑定", tags = {"模型管理"})
    @RequestMapping(value = "/model-mgt/model/modeldevice/delete", method = RequestMethod.POST)
    public BaseResp delModelDevice(Integer modelId) {
        return new BaseResp(ResultStatus.SUCCESS, modelService.delModelDevice(modelId));
    }
}
